package com.wl.application.controller;

import com.wl.application.common.all.Const;
import com.wl.application.common.all.Result;
import com.wl.application.entity.Permission;
import com.wl.application.entity.Role;
import com.wl.application.services.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.PublicKey;
import java.util.Date;
import java.util.List;

/**
 * @author wl
 * @version 1.0
 * @date 2022/6/21
 * @description:
 */
@RestController
@RequestMapping("/power")

public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @GetMapping("/list")
    public Result list(){
        return Result.ok(permissionService.list());
    }
    @PostMapping("/save")
    public Result save(@RequestBody Permission permission){
        permission.setCreateTime(new Date());
        permission.setPstate(Const.NO_ACTIVATE);
        return Result.ok("添加成功!",(permissionService.save(permission)));
    }
    @PutMapping("/update/state/{id}")
    public Result updateState(@PathVariable("id") Integer id){
        Permission byId = permissionService.getById(id);
        if(byId.getPstate().equals(Const.NO_ACTIVATE)){
            byId.setPstate(Const.ACTIVATE);
        }else{
            byId.setPstate(Const.NO_ACTIVATE);
        }
        return Result.ok(permissionService.updateById(byId));
    }

    @PutMapping("/update")
    public Result update(@RequestBody Permission permission){
        return Result.ok("修改成功!",permissionService.updateById(permission));
    }

    @DeleteMapping("/del")
    public Result delete(@RequestBody List<Integer> list){
        return Result.ok("删除成功!",permissionService.removeBatchByIds(list));
    }
    @GetMapping("/one/{id}")
    public Result getOne(@PathVariable Integer id){
        return Result.ok(permissionService.getById(id));
    }


}
